/*
 * Android Runner is a multiplayer GPS game fully written by Xurxo Mendez Perez
 * 
 * Copyright (C) 2009 Xurxo Mendez Perez
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.model.persistence.item.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;

import org.hibernate.annotations.AccessType;

import es.sonxurxo.androidrunner.model.persistence.game.entity.Game;
import es.sonxurxo.androidrunner.model.persistence.player.entity.Player;

/**
 * 
 * @author "Xurxo Mendez Perez"
 *
 */
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Item {

	private long itemId;
	private int points;
	private Game game;
	private Set<Player> playersCanSeeMe = new HashSet<Player>();
	private Set<Player> playersHaveMe = new HashSet<Player>();
	private int latitude;
	private int longitude;
	private String type = new String();
	
	public Item() {}
		
	public Item(Game game, int latitude, int longitude) {
		super();
		this.game = game;
		this.latitude = latitude;
		this.longitude = longitude;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "gameId")
	public Game getGame() {
		return game;
	}
	
	public void setGame(Game game) {
		this.game = game;
	}
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO,
            generator="ItemIdGenerator")
    @SequenceGenerator(             // It only takes effect for
            name="ItemIdGenerator", // databases providing identifier
            sequenceName="ItemSeq") // generators.
	public long getItemId() {
		return itemId;
	}
	
	public void setItemId(long itemId) {
		this.itemId = itemId;
	}
	
	@ManyToMany(
			fetch = FetchType.LAZY,
			mappedBy = "itemsICanSee",
			targetEntity = Player.class)
	@AccessType(value = "field")
	public Set<Player> getPlayersCanSeeMe() {
		return playersCanSeeMe;
	}
	
	public void setPlayersCanSeeMe(Set<Player> playersCanSeeMe) {
		this.playersCanSeeMe = playersCanSeeMe;
	}
	
	public void addPlayerCanSeeMe(Player player) {
		this.playersCanSeeMe.add(player);
		player.getItemsICanSee().add(this);
	}
	
	public void removePlayerCanSeeMe(Player player) {
		this.playersCanSeeMe.remove(player);
		player.getItemsICanSee().remove(this);
	}
	
	@ManyToMany(
			fetch = FetchType.LAZY,
			mappedBy = "itemsIHave",
			targetEntity = Player.class)
	public Set<Player> getPlayersHaveMe() {
		return playersHaveMe;
	}
	
	public void setPlayersHaveMe(Set<Player> playersHaveMe) {
		this.playersHaveMe = playersHaveMe;
	}
	
	public void addPlayerHaveMe(Player player) {
		this.playersHaveMe.add(player);
		player.getItemsIHave().add(this);
	}
	
	public void removePlayerHaveMe(Player player) {
		this.playersHaveMe.remove(player);
		player.getItemsIHave().remove(this);
	}
	
	public int getPoints() {
		return points;
	}

	public void setPoints(int points) {
		this.points = points;
	}
		
	public int getLatitude() {
		return latitude;
	}

	public void setLatitude(int latitude) {
		this.latitude = latitude;
	}

	public int getLongitude() {
		return longitude;
	}

	public void setLongitude(int longitude) {
		this.longitude = longitude;
	}

	public String toString() {
		return new String("itemId = " + this.itemId + " ;points = " + this.points);
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
